package com.cssw.fyzb.tps.api.lbs;

import com.cssw.fyzb.tps.client.LbsClient;
import com.cssw.fyzb.tps.model.vo.AddressVo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Feng Chen
 */
@Function(description = "经纬度转地址", parameters = {
        @Parameter(name = "lat", type = Type.String, description = "维度", required = true),
        @Parameter(name = "lng", type = Type.String, description = "经度", required = true),
}, result = @Result(properties = {
        @Property(name = "nation", type = Type.String, description = "国家"),
        @Property(name = "province", type = Type.String, description = "省"),
        @Property(name = "city", type = Type.String, description = "市"),
        @Property(name = "district", type = Type.String, description = "区"),
        @Property(name = "street", type = Type.String, description = "街道"),
}))
@Component
public class GetAddress implements Action {

    @Autowired
    private LbsClient client;

    private static final Logger logger = LoggerFactory.getLogger(GetAddress.class);

    @Override
    public Response execute(Request request) throws Exception {
        try {
            String lat = request.getString("lat");
            String lng = request.getString("lng");
            AddressVo address = client.getAddress(lat, lng);
            return Response.success().put("data", address);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("操作异常");
        }
    }

}
